from manger import db
from marshmallow import Schema, fields, post_load

class User(db.Model):

    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    sex = db.Column(db.Integer, nullable=False)

    def __repr__(self):
        return '<User %r>' % self.name

class UserSchema(Schema):
    class Meta:
        ordered = True
    # id = fields.Integer()
    # name = fields.String(validate=validate.Length(min=1))
    # sex = fields.Integer(required= True)
    name = fields.Str()
    sex = fields.Integer()

    @post_load  # 反序列化成一个对象，给schema的一个方法加上post_load装饰器
    def post_load(self, data, **kwargs):
        return User(**data)


